Ext.ns('AirApp');
AirApp.LocalAJAX = (function(){
	//private
	function getXMLHttpRequestObject() { 
		if(window.ActiveXObject) { 
			var msxmls = new Array( 
			'Microsoft.XMLHTTP',
			'Msxml2.XMLHTTP', 
			'Msxml2.XMLHTTP.4.0', 
			'Msxml2.XMLHTTP.3.0', 
			'Msxml2.XMLHTTP.5.0', 
			'Msxml2.XMLHTTP.6.0' ); 
			for (var i = 0; i < msxmls.length; i++) { 
			  try { 
				 return new ActiveXObject(msxmls[i]); 
			   } catch (e) {continue;} 
			}	
			
		}else if(window.XMLHttpRequest) { 
			return new XMLHttpRequest(); 
		} 
		throw new Error("Could not instantiate LocalXMLHttpRequest"); 
	} 
	return {
		dataFormatError : '服务器返回的数据模式有误',
		urlError : '未指定url',
		//url为空则读取当前文件
		get : function(url,jsonData,callback,scope,asyn){
			if(typeof url!=='string'){
				throw new Error(this.urlError);
			}
			//默认为异步请求
			var asynchronous = true;
			if(asyn===false){
				asynchronous = false;
			}
			var xhr = getXMLHttpRequestObject();
			xhr.onreadystatechange = function(){
				if(xhr.readyState==4 && asynchronous){
					if(scope){
						callback.call(scope,xhr);
					}else{
						callback(xhr);
					}
					xhr = null;
				}
			}
			xhr.open('GET', url, asynchronous);
			xhr.send(null);
			if(!asynchronous){
				if(scope){
					callback.call(scope,xhr);
				}else{
					callback(xhr);
				}
			}
			
		},
		
		getXml : function(url,jsonData,callback,scope,asyn){
			this.get(url,jsonData,function(xhr){
				if(scope){
					callback.call(scope,xhr.responseXML);
				}else{
					callback(xhr.responseXML);
				}
			},this,asyn);
		},
		
		getText : function(url,jsonData,callback,scope,asyn){
			this.get(url,jsonData,function(xhr){
				if(scope){
					callback.call(scope,xhr.responseText);
				}else{
					callback(xhr.responseText);
				}
			},this,asyn);
		},
		
		getJson : function(url,jsonData,callback,scope,asyn){
			this.get(url,jsonData,function(xhr){
				var json = null;
				try{
					json = eval("("+xhr.responseText+")");
				}catch(e){
					throw new Error(this.dataFormatError);
				}
				if(scope){
					callback.call(scope,json);
				}else{
					callback(json);
				}
			},this,asyn);
		}
	}
})()